﻿using System.Runtime.InteropServices;

namespace System.WindowsCe.Mvp.FormHelpers
{
	public static class SipExtensions
	{
		public static void ShowSip(this FormHelper helper)
		{
			ShowHideSip(true);
		}

		public static void HideSip(this FormHelper helper)
		{
			ShowHideSip(false);
		}

		public static bool SipShowed(this FormHelper helper)
		{
			SIPINFO s = new SIPINFO();
			SipGetInfo(s);
			return ((s.fdwFlags & 1) == SIPF_ON);
		}

		private static void ShowHideSip(bool enabled)
		{
			try
			{
				SipShowIM(enabled ? SIPF_ON : SIPF_OFF);
			}
			catch
			{
			}
		}

		public class SIPINFO
		{
			public SIPINFO()
			{
				cbSize = Marshal.SizeOf(typeof(SIPINFO));
			}

			public int cbSize;
			public int fdwFlags;
			public RECT rcVisibleDesktop;
			public RECT rcSipRect;
			public int dwImDataSize;
			public int pvImData;
		}

		public struct RECT
		{
			public int left;
			public int top;
			public int right;
			public int bottom;
		}

		const uint SIPF_OFF = 0x0;
		const uint SIPF_ON = 0x1;

		[DllImport("coredll.dll")]
		private extern static void SipShowIM(uint dwFlag);

		[DllImport("coredll.dll")]
		private extern static bool SipGetInfo(SIPINFO sipInfo);
	}
}